import coursewareApi from "@rc/apis/rc/courseware.mjs";

import filtrates from "./filtrates.mjs";

import EditDialog from "../components/courseware-edit/layout-dialog.vue";
import EditBase from "../components/courseware-edit/base.vue";
import EditProject from "../components/courseware-edit/project.vue";

export default {
	name: "file-wait",
	components: { EditDialog, EditBase, EditProject },
	data() {
		return {
			filtrates, // 筛选配置
			isLoading: false, // 页面加载状态
			baseLoading: false, // 基础信息编辑的加载状态
			projectLoading: false, // 项目信息编辑的加载状态
			isShowEditDialog: false, // 是否显示编辑弹窗
			isEditBatchStatus: false, // 当前是否为批量编辑状态
			searchParams: {}, // 当前的筛选项信息
			tableList: [], // 表格数据
			selectItems: [], // 当前表格选中的选项数组
			page: {
				// 分页数据
				pageIndex: 1,
				pageSize: 50,
				pageCount: 0
			},
			editInfo: {
				// 当前单个编辑的课件详情
				resourceId: "", //视频资源主键
				name: "", // 视频资源名称
				sectionNumber: "", // 节数
				sourceFileName: "", // 源文件名称
				videoVid: "", // 视频vid
				teacherId: "", // 授课老师标识
				professorName: "", // 授课老师名称
				livePlaybackState: "N", // 直播回放状态
				rcDisposeResourceVideoProjectResponseVos: [] // 项目列表
			},
			sortType: "N",
		};
	},
	computed: {
		// 当前选中的表格id集合
		selectIds() {
			return this.selectItems.map((i) => i.resourceId);
		},
		// 当前编辑的课件基础详情
		editBaseInfo() {
			if (this.isEditBatchStatus) {
				// 批量编辑状态，传入空值
				return {
					sectionNumber: "",
					name: "",
					teacherId: "",
					professorName: "",
					sourceFileName: "",
					videoVid: ""
				};
			} else {
				// 单个编辑状态，传入详情
				return {
					sectionNumber: this.editInfo.sectionNumber,
					name: "",
					teacherId: this.editInfo.teacherId,
					professorName: this.editInfo.professorName,
					sourceFileName: this.editInfo.sourceFileName,
					videoVid: this.editInfo.videoVid,
					livePlaybackState: this.editInfo.livePlaybackState,
				};
			}
		},
		// 当前编辑的课件项目详情
		editProjectInfo() {
			if (this.isEditBatchStatus) {
				// 批量编辑状态，传入空值
				return [];
			} else {
				// 单个编辑状态，传入详情
				return this.editInfo.rcDisposeResourceVideoProjectResponseVos.map((i) => ({
					...i,
					projectId: [i.projectId],
					subjectList: i.subjectList.map((i2) => i2.subjectId),
					subjectFullList: i.subjectList,
					seriesCourseList: i.seriesCourseList.map((i2) => i2.seriesCourseId)
				}));
			}
		}
	},
	methods: {
		/**
		 * 根据当前检索条件查询表格数据
		 * @param {boolean} isKeepChecked 数据更新后表格是否保留之前的选中状态
		 */
		queryTableData(isKeepChecked = false) {
			this.isLoading = true;
			console.warn(this.searchParams);
			const dt = { ...this.searchParams, sortType: this.sortType };
			if (this.searchParams.uploadDate && this.searchParams.uploadDate.start && this.searchParams.uploadDate.end) {
				dt.starTime = this.searchParams.uploadDate.start + " 00:00:00";
				dt.endTime = this.searchParams.uploadDate.end + " 23:59:59";
			}
			delete dt.uploadDate;
			coursewareApi.queryFileWaitList({
				data: {
					pageSize: this.page.pageSize,
					pageIndex: PageUtil.getStartIndex(this.page),
					...dt
				},
				success: ({ count, data }) => {
					this.tableList = data;
					this.page.pageCount = count;
					if (isKeepChecked) {
						// 数据更新完成后选中之前的选中项目
						this.$nextTick(() => {
							const selectItems = [];
							this.tableList.forEach((i1) => {
								if (this.selectIds.some((i2) => i2 === i1.resourceId)) {
									selectItems.push(i1);
									this.$refs.table.toggleRowSelection(i1, true);
								}
							});
							this.selectItems = selectItems;
						});
					} else {
						this.selectItems = [];
					}
				},
				complete: () => {
					this.isLoading = false;
				}
			});
		},
		// 筛选搜索
		filterSubmit(params) {
			this.searchParams = params;
			this.queryTableData();
		},
		// 复制文本到剪贴板
		copyText(text) {
			if (StringUtil.copyText(text)) {
				this.$message.success("vid复制成功");
			} else {
				this.$message.error("vid复制失败");
			}
		},
		// 表格选中项改变
		selectionChange(arr) {
			this.selectItems = arr;
		},
		// 单个切换直播回放状态
		playbackSwitch(item) {
			coursewareApi.editPlaybackStatus({
				data: {
					resourceId: item.resourceId,
					livePlaybackState: item.livePlaybackState === "Y" ? "N" : "Y"
				},
				success: () => {
					this.$message.success("修改成功");
					item.livePlaybackState = item.livePlaybackState === "Y" ? "N" : "Y";
				}
			});
		},
		/**
		 * 批量设置为直播回放
		 * @param {string} state Y设置开启，N设置关闭
		 */
		setPlaybackBatch(state) {
			if (!this.selectIds.length) {
				this.$message.warning("请先选择课件");
				return;
			}
			coursewareApi.editPlaybackStatusBatch({
				data: {
					resourceIds: this.selectIds,
					livePlaybackState: state
				},
				success: () => {
					this.$message.success("批量设置成功");
					this.queryTableData(true);
				}
			});
		},
		/**
		 * 批量进入待审
		 * @param {string[]} ids 需要进入待审的id集合
		 */
		waitAuditBatch(ids = []) {
			if (!ids.length) {
				this.$message.warning("请先选择课件");
				return;
			}
			// 根据传入的id集合检索出表格中所有的匹配项
			const items = this.tableList.filter((i1) => ids.some((i2) => i2 === i1.resourceId));
			// 存在匹配不到创建人的
			if (items.some((i) => !i.createUserName)) {
				this.$message.warning("请至“课件上传账号”栏目设置保利威与优路的账号关系");
				return;
			}
			// 存在未转码成功的
			if (items.some((i) => i.escapeState !== "ECD")) {
				this.$message.warning("课件转码成功才可进入待审，您可先编辑课件信息");
				return;
			}
			// 存在编辑状态为未编辑的
			if (items.some((i) => i.editState === "N")) {
				this.$message.warning("请先编辑课件信息");
				return;
			}
			coursewareApi.waitAuditBatch({
				data: {
					resourceIds: ids
				},
				success: () => {
					this.$message.success("操作成功");
					this.queryTableData();
					this.isShowEditDialog = false;
				}
			});
		},
		// 批量删除待处理课件
		delFileWaitBatch(delArr = []) {
			if (!delArr.length) {
				this.$message.warning("请先选择课件");
				return;
			}
			this.$confirm("删除后，已编辑信息均会删除且无法恢复？", "提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning",
				callback: (action) => {
					if (action === "confirm") {
						coursewareApi.delFileWaitBatch({
							data: {
								resourceIds: delArr
							},
							success: () => {
								this.$message.success("删除成功");
								this.queryTableData();
							}
						});
					}
				}
			});
		},
		// 点击单个修改
		onEdit(resourceId) {
			coursewareApi.queryFileInfo({
				data: { resourceId },
				success: (data) => {
					this.isEditBatchStatus = false;
					this.editInfo = data;
					this.isShowEditDialog = true;
				}
			});
		},
		// 点击批量修改
		onEditBatch() {
			if (!this.selectIds.length) {
				this.$message.warning("请先选择课件");
				return;
			}
			this.isEditBatchStatus = true;
			this.isShowEditDialog = true;
		},
		/**
		 * 编辑弹窗,基础信息部分点击提交
		 * @param {object} data 表单基础数据
		 * @param {object} itemsDetail 表单内的下拉选中详情
		 */
		baseEditSubmit(data, itemsDetail) {
			// 批量编辑和单个编辑相同的字段部分
			const params = {
				teacherId: data.teacherId,
				professorId: itemsDetail.teacher.userId,
				professorName: itemsDetail.teacher.userName,
				professorDptName: itemsDetail.teacher.dptName || "无",
                livePlaybackState: data.livePlaybackState,
			};
			this.baseLoading = true;
			if (this.isEditBatchStatus) {
				// 当前是批量编辑
				coursewareApi.editResourceBaseBatch({
					data: {
						...params,
						resourceIds: this.selectIds
					},
					success: () => {
						this.$message.success("修改成功");
						this.queryTableData(true);
					},
					complete: () => {
						this.baseLoading = false;
					}
				});
			} else {
				// 当前是单个编辑
				coursewareApi.editResourceBase({
					data: {
						...params,
						resourceId: this.editInfo.resourceId,
						name: data.name,
						sectionNumber: data.sectionNumber,
						auditState: "D", // "D" 待处理
						sourceFileName: data.sourceFileName
					},
					success: () => {
						this.$message.success("修改成功");
						this.queryTableData(true);
					},
					complete: () => {
						this.baseLoading = false;
					}
				});
			}
		},
		/**
		 * 编辑弹窗,项目信息部分点击提交
		 * @param {object} data 表单基础数据
		 * @param {object} itemsDetail 表单内的下拉选中详情
		 * @param {function} callback 提交回调，用于告诉子组件是否提交成功
		 */
		projectEditSubmit(data, itemsDetail, callback) {
			const { project, subject, course, year, period } = itemsDetail;
			const courseEnd = course.filter((i) => i.level === 2);
			// 批量编辑和单个编辑相同的字段部分
			const params = {
				projectId: data.projectId,
				projectName: project.find((i) => i.value === data.projectId).label,
				examCycleId: period.pypId,
				examCycleName: period.pypseqName,
				subjectList: subject.map((i) => ({ subjectId: i.subjectId, subjectName: i.subjectName })),
				seriesCourseList: courseEnd.map((i) => ({
					seriesCourseId: i.value,
					seriesCourseName: i.label,
					teachRange: i.parent.value
				})),
				applicableYear: year.projectyearYear,
				applicableYearId: year.projectyearId
			};
			this.projectLoading = true;
			if (this.isEditBatchStatus) {
				// 当前是批量编辑
				coursewareApi.editFileProjectBatch({
					data: {
						...params,
						resourceIds: this.selectIds
					},
					success: () => {
						callback(true);
						this.$message.success("修改成功");
						this.queryTableData(true);
					},
					complete: () => {
						this.projectLoading = false;
					}
				});
			} else {
				// 当前是单个编辑
				coursewareApi.editFileProject({
					data: {
						...params,
						resourceId: this.editInfo.resourceId,
						originalProjectId: data.originalProjectId || data.projectId
					},
					success: () => {
						callback(true);
						this.$message.success("修改成功");
						this.queryTableData();
					},
					complete: () => {
						this.projectLoading = false;
					}
				});
			}
		},
		/**
		 * 编辑弹窗,项目信息部分已保存的项目点击删除
		 * @param {string} projectId 要删除的项目id
		 * @param {function} callback 提交回调，用于告诉子组件是否提交成功
		 */
		projectEditDelete(projectId, callback) {
			this.projectLoading = true;
			coursewareApi.delFileProjectBatch({
				data: {
					projectId,
					resourceIds: this.isEditBatchStatus ? this.selectIds : [this.editInfo.resourceId]
				},
				success: () => {
					callback(true);
					this.$message.success("删除成功");
				},
				complete: () => {
					this.projectLoading = false;
				}
			});
		},
		sortChange({ order }) {
			// ascending,descending
			if (order === "ascending") {
				this.sortType = "Y";
			} else if (order === "descending") {
				this.sortType = "N";
			} else {
				this.sortType = "N";
			}
			this.queryTableData();
		},
	},
	created() {
		PermissUtil.checkPagePermiss("rc/courseware/wait-list");
	}
};
